﻿using System.Collections.Generic;
using DarkSky.Microdata.Models;
using DarkSky.Microdata.Settings;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Tokens;

namespace DarkSky.Microdata.Drivers {
    public class MicrodataPartDriver : ContentPartDriver<MicrodataPart> {
        private readonly ITokenizer _tokenizer;
        public MicrodataPartDriver(ITokenizer tokenizer) {
            _tokenizer = tokenizer;
        }

        protected override DriverResult Display(MicrodataPart part, string displayType, dynamic shapeHelper) {
            return ContentShape("Parts_Microdata", () => {
                var entries = part.TypePartDefinition.Settings.GetModel<MicrodataPartSettings>().Entries;
                foreach (var entry in entries) {
                    entry.Value = _tokenizer.Replace(entry.Value, BuildTokenContext(part.ContentItem), new ReplaceOptions { Encoding = ReplaceOptions.NoEncode});
                }
                return shapeHelper.Parts_Microdata(Entries: entries);
            });
        }

        private IDictionary<string, object> BuildTokenContext(IContent item) {
            return new Dictionary<string, object> { { "Content", item } };
        }
    }
}